using Todos.Application.Users.Specs;

namespace Todos.Application.Users.Queries;

[AuthConfig(typeof(User), ResourceAccessType.RoleLimit, "GetUserNode")]
public class GetUserNodeQuery(string? keyword) : SingleQuery<UserNodeDto[]>
{
    public string? Keyword { get; set; } = keyword;
}

public class GetUserNodeQueryHandler(IQueryRepository<User> queryRepository) : IRequestHandler<GetUserNodeQuery, UserNodeDto[]>
{
    public async Task<UserNodeDto[]> Handle(GetUserNodeQuery request, CancellationToken cancellationToken)
    {
        var spec = new UserUnlockedSpec().And(new UserByKeywordSpec(request.Keyword));
        var nodes = await queryRepository.GetAllAsync<UserNodeDto>(spec, cancellationToken);
        return [.. nodes];
    }
}

[MapFrom(typeof(User))]
public class UserNodeDto
{
    public long Id { get; set; }
    public required string UserName { get; set; }
    public string? NickName { get; set; }
}

